<!DOCTYPE html><html lang="zh-CN"><head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>humanize() | Moment.js &#x6587;&#x6863;</title>
  <link rel="stylesheet" href="static/css/style.css">
  <link rel="shortcut icon" href="http://momentjs.cn/static/img/moment-favicon.png">
  <link rel="stylesheet" href="static/css/atom-one-light.min.css">
</head>
<body class="is-moment">
  <div class="hero hero-oneline">
    <div class="hero-centered">
      <h1>humanize()</h1>
    </div>
  </div>
  <div id="api-section-__FILENAME__">
    <div id="content" class="clearfix">
  
      <div id="column1" data-id="__ID__" class="interior">
        <header>
          <!-- <h1>humanize()</h1> -->
          <div id="gtoc">
            <ul>
              <!-- <li>v</li> -->
              <li>
                <a href="index.html#/durations/humanize/" name="toc">&#x8FD4;&#x56DE;&#x4E0A;&#x5C42;&#x6587;&#x6863;</a>
              </li>
              <li style="border: none;">
                <a href="javascript:window.open('https://github.com/momentjscn/moment-docs-cn/edit/master/moment/08-durations/03-humanize.md');" rel="nofollow" target="_blank">&#x63D0;&#x4EA4;&#x4FEE;&#x6539;</a>
              </li>
              <!-- <li>
                <a href="/search__SEARCH_PARAM__" name="toc">搜索</a>
              </li> -->
            </ul>
          </div>
          <hr>
        </header>

        <div id="biz_item"></div>
  
        <div id="apicontent">
          <div id="content_left">
							<div class="docs-method-signature">
								<pre><code class="language-js">moment.duration().humanize();
</code></pre>

							</div>
<p>&#x6709;&#x65F6;&#xFF0C;&#x53EA;&#x60F3;&#x8981; <code>moment#from</code> &#x7684;&#x6240;&#x6709;&#x4F18;&#x70B9;&#xFF0C;&#x4F46;&#x53C8;&#x4E0D;&#x60F3;&#x521B;&#x5EFA;&#x4E24;&#x4E2A; moment&#xFF0C;&#x800C;&#x53EA;&#x60F3;&#x663E;&#x793A;&#x4E00;&#x6BB5;&#x65F6;&#x957F;&#x3002;</p>
<p>&#x4F7F;&#x7528; <code>moment.duration().humanize()</code>&#x3002;</p>
<pre><code class="language-javascript">moment.duration(1, &quot;minutes&quot;).humanize(); // 1 &#x5206;&#x949F;
moment.duration(2, &quot;minutes&quot;).humanize(); // 2 &#x5206;&#x949F;
moment.duration(24, &quot;hours&quot;).humanize();  // 1 &#x5929;
</code></pre>
<p>&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x8FD4;&#x56DE;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x6CA1;&#x6709;&#x540E;&#x7F00;&#x3002;
&#x5982;&#x679C;&#x9700;&#x8981;&#x540E;&#x7F00;&#xFF0C;&#x5219;&#x6309;&#x5982;&#x4E0B;&#x6240;&#x793A;&#x4F20;&#x5165; true&#x3002;</p>
<pre><code class="language-javascript">moment.duration(1, &quot;minutes&quot;).humanize(true); // 1 &#x5206;&#x949F;&#x5185;
</code></pre>
<p>&#x5BF9;&#x4E8E;&#x5F53;&#x524D;&#x65F6;&#x95F4;&#x4E4B;&#x524D;&#x7684;&#x540E;&#x7F00;&#xFF0C;&#x5219;&#x4F20;&#x5165;&#x8D1F;&#x6570;&#x3002;</p>
<pre><code class="language-javascript">moment.duration(-1, &quot;minutes&quot;).humanize(true); // 1 &#x5206;&#x949F;&#x524D;
</code></pre>
<p>&#x65E0;&#x6548;&#x7684;&#x65F6;&#x957F;&#x4F1A;&#x88AB;&#x4EBA;&#x6027;&#x5316;&#x4E3A; <code>Invalid Date</code> &#x7684;&#x672C;&#x5730;&#x5316;&#x7248;&#x672C;&#x3002;</p>
<pre><code class="language-javascript">moment.duration.invalid().humanize(); // Invalid Date
</code></pre>
					</div>
          <div id="content_right">
							<div class="docs-method-signature">
								<pre><code class="language-js">moment.duration().humanize();
</code></pre>

							</div>
<p>Sometimes, you want all the goodness of <code>moment#from</code> but you don&apos;t want to have to create two moments, you just want to display a length of time.</p>
<p>Enter <code>moment.duration().humanize()</code>.</p>
<pre><code class="language-javascript">moment.duration(1, &quot;minutes&quot;).humanize(); // a minute
moment.duration(2, &quot;minutes&quot;).humanize(); // 2 minutes
moment.duration(24, &quot;hours&quot;).humanize();  // a day
</code></pre>
<p>By default, the return string is suffixless. If you want a suffix, pass in true as seen below.</p>
<pre><code class="language-javascript">moment.duration(1, &quot;minutes&quot;).humanize(true); // in a minute
</code></pre>
<p>For suffixes before now, pass in a negative number.</p>
<pre><code class="language-javascript">moment.duration(-1, &quot;minutes&quot;).humanize(true); // a minute ago
</code></pre>
<p>Invalid durations are humanized to the localized version of <code>Invalid Date</code>.</p>
<pre><code class="language-javascript">moment.duration.invalid().humanize(); // Invalid Date
</code></pre>
					</div>
          <div></div>
        </div>
      </div>
    </div>
  </div>
  
  <script src="static/js/highlight.min.js"></script>
  <script src="static/js/javascript.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script>
  <script>var _hmt = _hmt || []; (function () { var hm = document.createElement("script"); hm.src = "//hm.baidu.com/hm.js?7a7f78c805fbacc7e075d34f153031aa"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })();</script>

  <style>
    .hero-centered {
      margin: 0;
          margin-left: 10px;
    }
#gtoc ul {
  list-style: none;
  margin-left: 0;
  line-height: 1.5rem;
  padding: 0;
}
#gtoc>ul>li {
  display: inline;
  border-right: 1px solid #000;
  margin-right: .4rem;
  padding-right: .4rem;
}
#gtoc>ul>li>a {
  color: #61b2a7;
}
#content_left {
  width: 49%;float: left;border-right: 1px solid #eee;    padding-right: 2%;
}
#content_right {
float: left;width: 48%;margin-left: 3%;
}
#column1 {
  padding: 0 2rem;
}
  </style>


</body></html>